import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.sf.image4j.codec.ico.ICODecoder;

public class ToolBox {

	static BufferedImage img_icon;

	public static BufferedImage reSizeIco(File f, int size) throws IOException {

		img_icon = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics2D = img_icon.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(ICODecoder.read(f).get(0), 0, 0, size, size, null);
		graphics2D.dispose();
		return img_icon;
	}

	public static BufferedImage reSizeIco(BufferedImage i, int size)
			throws IOException {

		img_icon = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics2D = img_icon.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(i, 0, 0, size, size, null);
		graphics2D.dispose();
		return img_icon;
	}

	public static BufferedImage reSizeImg(BufferedImage i, int size)
			throws IOException {
		double x;
		double y;
		int ix = i.getWidth();
		int iy = i.getHeight();

		if (iy > ix) {
			y = size;
			x = (size * 1.0) / (iy / (ix * 1.0));

		} else {
			x = size;
			y = size * 1.0 / (ix / (iy * 1.0));
		}

		img_icon = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics2D = img_icon.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(i, 0, 0, (int) x, (int) y, null);
		graphics2D.dispose();
		return img_icon;

	}

	public static BufferedImage reSizeImg(File f, int size) throws IOException {
		double x;
		double y;

		BufferedImage i = ImageIO.read(f);

		int ix = i.getWidth();
		int iy = i.getHeight();

		if (iy > ix) {
			y = size;
			x = (size * 1.0) / (iy / (ix * 1.0));

		} else {
			x = size;
			y = size * 1.0 / (ix / (iy * 1.0));
		}

		img_icon = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics2D = img_icon.createGraphics();
		graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics2D.drawImage(i, 0, 0, (int) x, (int) y, null);
		graphics2D.dispose();
		return img_icon;

	}
}
